using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;

using miciv.Job;
using micivCore.Data;

namespace micivCore.Job
{
    public delegate bool ApplyTechniqueHandler(IUnit unit);
    public delegate void ApplyTechniqueCompletedHandler(bool _bResult);

    public class ApplyTechniqueJob : IJob
    {
        protected IUnitTechnique m_technique;
        public IUnitTechnique Technique
        {
            get
            {
                return this.m_technique;
            }
        }

        protected IUnit m_unit;
        public IUnit Unit
        {
            get
            {
                return this.m_unit;
            }
        }

        protected ApplyTechniqueCompletedHandler m_callback;
        public ApplyTechniqueCompletedHandler Callback
        {
            get
            {
                return this.m_callback;
            }
        }

        protected ApplyTechniqueHandler m_job;
        protected bool m_bIsRunning;
        protected bool m_bIsCompleted;
        protected string m_strID;

        public ApplyTechniqueJob(IUnitTechnique _technique, IUnit _unit, ApplyTechniqueCompletedHandler _callback)
        {
            this.m_technique = _technique;
            this.m_unit = _unit;
            this.m_callback = _callback;
            this.m_job = null;
            this.m_bIsRunning = false;
            this.m_bIsCompleted = false;
            this.m_strID = string.Format("{0}=>{1}", _technique.ID, _unit.ID);
        }

        protected void CallbackFunc(IAsyncResult _iAsyncResult)
        {
            if (null != this.Callback)
            {
                bool bResult = false;
                if (false != _iAsyncResult.IsCompleted)
                {
                    AsyncResult asyncResult = _iAsyncResult as AsyncResult;
                    bResult = (asyncResult.AsyncDelegate as ApplyTechniqueHandler).EndInvoke(_iAsyncResult);
                }
                this.Callback(bResult);
            }
            this.m_bIsCompleted = true;
        }

        #region IJob Membres

        virtual public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        virtual public bool IsRunning
        {
            get
            {
                return this.m_bIsRunning;
            }
        }

        virtual public bool IsCompleted
        {
            get
            {
                return this.m_bIsCompleted;
            }
        }

        virtual public void Run()
        {
            if ((false == this.IsRunning) && (false == this.IsCompleted))
            {
                this.m_job = new ApplyTechniqueHandler(this.Technique.Apply);
                this.m_job.BeginInvoke(this.Unit, this.CallbackFunc, null);
                this.m_bIsRunning = true;
            }
        }

        #endregion
    }
}
